﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using wojilu.Apps.Qycms.Domain;
using wojilu.IO;

namespace wojilu.Web.Controller
{
    public class YBBCommon
    {
        /// <summary>
        /// 系统设置缓存key
        /// </summary>
        public const string cacheconfigkey = "/YBBWJLQYCMS/YBBConfig";

        /// <summary>
        /// 网站语言包缓存key
        /// </summary>
        public const string cachelngpackkey = "/YBBWJLQYCMS/YBBLngPack";

        /// <summary>
        /// 网站菜单缓存key
        /// </summary>
        public const string cachemenukey = "/YBBWJLQYCMS/YBBMenu";

        /// <summary>
        /// 留言菜单缓存key
        /// </summary>
        public const string cacheformmenukey = "/YBBWJLQYCMS/YBBFormMenu";

        /// <summary>
        /// 网站网站主题缓存key
        /// </summary>
        public const string cacheskinkey = "/YBBWJLQYCMS/YBBSkin";

        /// <summary>
        /// 指定广告或者友情链接缓存key
        /// </summary>
        public const string cacheadvertlistkey = "/YBBWJLQYCMS/AdvertList";

        /// <summary>
        /// 在线客服缓存key
        /// </summary>
        public const string cachecalingistkey = "/YBBWJLQYCMS/CallingList";

        /// <summary>
        /// 获取内容分表内容
        /// </summary>
        public const string cachereadattdrkey = "/YBBWJLQYCMS/ReadAttDr";

        /// <summary>
        /// 获取分类内容
        /// </summary>
        public const string cachelistmenukey = "/YBBWJLQYCMS/ListMenu";

        /// <summary>
        /// 获取网站主题
        /// </summary>
        /// <returns></returns>
        public static Skin GetDefaultSkin()
        {
            var cachekey = cacheskinkey;
            YBBCache cacheService = YBBCache.GetCacheService();
            object obj2 = cacheService.RetrieveObject(cachekey);
            if (obj2 == null)
            {
                obj2 = Skin.find(" isclass=:i").set("i", 1).first();
                cacheService.AddObject(cachekey, obj2, 0);
            }
            return (Skin)obj2;
        }

        /// <summary>
        /// 获取语言包
        /// </summary>
        /// <param name="lngs"></param>
        /// <param name="keycode"></param>
        /// <returns></returns>
        public static String GetLangStr(String lngs, String keycode)
        {
            var cachekey = cachelngpackkey;
            YBBCache cacheService = YBBCache.GetCacheService();
            object obj2 = cacheService.RetrieveObject(cachekey);
            if (obj2 == null)
            {
                obj2 = Lngpack.findAll();
                cacheService.AddObject(cachekey, obj2, 0);
            }
            var l = ((List<Lngpack>)obj2).Find(p => p.keycode == keycode && p.lng == lngs);
            return l != null ? l.langstr : String.Empty;
        }

        /// <summary>
        /// 获取设置内容
        /// </summary>
        /// <returns></returns>
        public static String GetConfigStr(String valname)
        {
            var cachekey = cacheconfigkey;
            YBBCache cacheService = YBBCache.GetCacheService();
            object obj2 = cacheService.RetrieveObject(cachekey);
            if (obj2 == null)
            {
                obj2 = Configs.findAll();
                cacheService.AddObject(cachekey, obj2, 0);
            }
            var c = ((List<Configs>)obj2).Find(p => p.valname == valname);
            return c != null ? c.value : String.Empty;
        }

        /// <summary>
        /// 获取指定分类导航链接
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static TypeList GetMenu(Int64 Id)
        {
            var cachekey = cachemenukey + Id;
            YBBCache cacheService = YBBCache.GetCacheService();
            object obj2 = cacheService.RetrieveObject(cachekey);
            if (obj2 == null)
            {
                var t = TypeList.findById(Id);
                if (t == null)
                {
                    t = new TypeList();
                }
                obj2 = t;
                cacheService.AddObject(cachekey, obj2, 0);
            }
            return (TypeList)obj2;
        }

        /// <summary>
        /// 获取指定留言导航链接
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static Bbs_TypeList GetFormMenu(Int64 Id)
        {
            var cachekey = cacheformmenukey + Id;
            YBBCache cacheService = YBBCache.GetCacheService();
            object obj2 = cacheService.RetrieveObject(cachekey);
            if (obj2 == null)
            {
                var t = Bbs_TypeList.findById(Id);
                if (t == null)
                {
                    t = new Bbs_TypeList();
                }
                obj2 = t;
                cacheService.AddObject(cachekey, obj2, 0);
            }
            return (Bbs_TypeList)obj2;
        }

        /// <summary>
        /// 获取指定分类下的所有子分类
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static List<TypeList> GetListMenu(Int64 Id)
        {
            var cachekey = cachelistmenukey + Id;
            YBBCache cacheService = YBBCache.GetCacheService();
            object obj2 = cacheService.RetrieveObject(cachekey);
            if (obj2 == null)
            {
                obj2 = TypeList.find(" upid=" + Id + " and isshow = 1 order by pid asc,id asc").list();
                cacheService.AddObject(cachekey, obj2, 0);
            }
            return (List<TypeList>)obj2;
        }

        /// <summary>
        /// 获取当前分类下的所有子分类
        /// </summary>
        /// <param name="typeread"></param>
        /// <returns></returns>
        public static List<TypeList> GetListMenu(TypeList typeread)
        {
            var cachekey = cachelistmenukey + "typeread" + typeread.Id;
            YBBCache cacheService = YBBCache.GetCacheService();
            object obj2 = cacheService.RetrieveObject(cachekey);
            if (obj2 == null)
            {
                var list = new List<TypeList>();
                if (typeread.upid == 0)
                {
                    list = TypeList.find(" upid = " + typeread.Id + " and isshow = 1 order by pid asc,id asc").list();
                }
                else
                {
                    list = TypeList.find(" upid = " + typeread.Id + " and isshow = 1 order by pid asc,id asc").list();
                    if (list.Count == 0)
                    {
                        list = TypeList.find(" upid = " + typeread.upid + " and isshow = 1 order by pid asc,id asc").list();
                    }
                }
                obj2 = list;
                cacheService.AddObject(cachekey, obj2, 0);
            }
            return (List<TypeList>)obj2;
        }

        /// <summary>
        /// 获取指定分类下的指定条数分类
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Num"></param>
        /// <returns></returns>
        public static List<TypeList> GetListMenu(Int64 Id, Int32 Num)
        {
            var pagelist = TypeList.findPage(" upid=" + Id + " and isshow = 1 order by pid asc,id asc", Num).Results;
            return pagelist;
        }

        /// <summary>
        /// 获取内容常规数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static Document GetRead(Int64 Id)
        {
            return Document.findById(Id);
        }

        /// <summary>
        /// 获取指定内容的多表数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static DataRow GetReadDr(Int64 Id)
        {
            var typeread = TypeList.findById(Id);
            var read = db.RunTable<Document>("select ybb_document.*,ybb_document_content.*,ybb_document_attr.* from ybb_document left join ybb_document_content on ybb_document.id=ybb_document_content.did left join ybb_document_attr on ybb_document.id=ybb_document_attr.did where ybb_document.id=" + typeread.linkid);
            return read.Rows[0];
        }

        public static DataRow GetReadAttDr(Int64 Id)
        {
            var cachekey = cachereadattdrkey + Id;
            YBBCache cacheService = YBBCache.GetCacheService();
            object obj2 = cacheService.RetrieveObject(cachekey);
            if (obj2 == null)
            {
                var read = db.RunTable<Document>("select * from ybb_document_attr where did=" + Id);
                if (read.Rows.Count > 0)
                {
                    obj2 = read.Rows[0];
                }
                else
                {
                    obj2 = null;
                }
                cacheService.AddObject(cachekey, obj2, 0);
            }
            return (DataRow)obj2;
        }

        /// <summary>
        /// 获取指定内容指定推荐位的数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static List<Document> GetReadDt(Int64 Id, Int32 Recommend, Int32 Num)
        {
            var titlelist = new YBBCommon().GetChildStr(Id, true);
            var read = Document.findPage(" tid in (" + titlelist + ")  and (recommend like '%," + Recommend + ",%' or recommend like '" + Recommend + ",%' or recommend='" + Recommend + "' or recommend like  '%," + Recommend + "')  order by pid asc,id desc", Num).Results;
            return read;
        }

        /// <summary>
        /// 获取指定内容的数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static List<Document> GetReadDt(String Id, Int32 Recommend, Int32 Num)
        {
            var strarray = Id.SplitInt(",");
            var titlelist = String.Empty;
            foreach (var row in strarray)
            {
                titlelist += new YBBCommon().GetChildStr(row, true) + ",";
            }
            if (titlelist.EndsWith(","))
            {
                titlelist = titlelist.Remove(titlelist.Length - 1, 1);
            }
            var read = Document.findPage(" tid in(" + titlelist + ") and (recommend like '%," + Recommend + ",%' or recommend like '" + Recommend + ",%' or recommend='" + Recommend + "' or recommend like  '%," + Recommend + "')  order by pid asc,id desc", Num).Results;
            return read;
        }

        /// <summary>
        /// 获取指定分类的内容
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Num"></param>
        /// <returns></returns>
        public static List<Document> GetListRead(Int64 Id, Int32 Num)
        {
            var titlelist = new YBBCommon().GetChildStr(Id, true);
            var pagelist = Document.findPage(" tid in (" + titlelist + ")  and mid <>8 or (extid like '%," + Id + ",%' or extid like '" + Id + ",%' or extid='" + Id + "' or extid like  '%," + Id + "') order by pid asc,addtime desc", Num).Results;
            return pagelist;
        }

        /// <summary>
        /// 获取指定分类的内容
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Num"></param>
        /// <returns></returns>
        public static List<Document> GetListRead(String Id, Int32 Num)
        {
            var strarray = Id.SplitInt(",");
            var titlelist = String.Empty;
            foreach (var row in strarray)
            {
                titlelist += new YBBCommon().GetChildStr(row, true) + ",";
            }
            if (titlelist.EndsWith(","))
            {
                titlelist = titlelist.Remove(titlelist.Length - 1, 1);
            }
            var pagelist = Document.findPage(" tid in (" + titlelist + ") and mid <>8 or (extid like '%," + Id + ",%' or extid like '" + Id + ",%' or extid='" + Id + "' or extid like  '%," + Id + "') order by pid asc,addtime desc", Num).Results;
            return pagelist;
        }

        /// <summary>
        /// 获取关联相册图片
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static List<Document_Album> GetListDAlbum(Int64 Id)
        {
            var list = Document_Album.find(" did =:d").set("d", Id).list();
            return list;
        }

        /// <summary>
        /// 获取指定广告或者友情链接
        /// </summary>
        /// <returns></returns>
        public static List<Advert> GetAdvertList(Int64 Id, Int32 Num, String Lngs, Int32 AdType)
        {
            var cachekey = cacheadvertlistkey + Id + Num + Lngs + AdType;
            YBBCache cacheService = YBBCache.GetCacheService();
            object obj2 = cacheService.RetrieveObject(cachekey);
            if (obj2 == null)
            {
                var condition = " atid =" + Id + " and lng='" + Lngs + "'";
                if (AdType > 0)
                {
                    condition += " and adtype =" + AdType + "";
                }
                condition += " and (istime=0 || (istime=1 and starttime<'" + DateTime.Now + "' and endtime>'" + DateTime.Now + "'))";
                condition += " order by pid asc,id asc";
                obj2 = Advert.findPage(condition, Num).Results;
                cacheService.AddObject(cachekey, obj2, 0);
            }
            return (List<Advert>)obj2;
        }

        /// <summary>
        /// 获取在线客服
        /// </summary>
        /// <returns></returns>
        public static List<m_qq> GetCallingList(String Lngs)
        {
            var cachekey = cachecalingistkey + Lngs;
            YBBCache cacheService = YBBCache.GetCacheService();
            object obj2 = cacheService.RetrieveObject(cachekey);
            if (obj2 == null)
            {
                var c = Calling.find(" lng =:l and isclass=1 order by pid asc,id asc").set("l", Lngs).list();
                var list = new List<m_qq>();
                foreach (var row in c)
                {
                    var code = String.Empty;
                    switch (row.type)
                    {
                        case 1:
                            code = row.code;
                            break;

                        case 2:
                            code = "<a target=\"_blank\" href=\"http://amos.im.alisoft.com/msg.aw?v=2&uid=" + row.code + "&site=cntaobao&s=" + row.style + "&charset=utf-8\" ><img border=\"0\" src=\"http://amos.im.alisoft.com/online.aw?v=2&uid=" + row.code + "&site=cntaobao&s=" + row.style + "&charset=utf-8\"/></a>";
                            break;

                        case 3:
                            if (row.style > 0)
                            {
                                switch (row.style)
                                {
                                    case 1:
                                        code = "<a href=\"skype:" + row.code + "?call\"><img src=\"http://mystatus.skype.com/smallclassic/" + row.code + "\"/></a>";
                                        break;

                                    case 2:
                                        code = "<a href=\"skype:" + row.code + "?call\"><img src=\"http://mystatus.skype.com/bigclassic/" + row.code + "\" width=\"110\"/></a>";
                                        break;

                                    case 3:
                                        code = "<a href=\"skype:" + row.code + "?call\"><img src=\"http://mystatus.skype.com/mediumicon/" + row.code + "\" />" + row.code + "</a>";
                                        break;

                                    case 4:
                                        code = "<a href=\"skype:" + row.code + "?call\"><img src=\"http://mystatus.skype.com/balloon/" + row.code + "\"  width=\"110\"/></a>";
                                        break;

                                }

                            }
                            break;

                        case 4:
                            code = "<a target=\"_blank\" href=\"http://settings.messenger.live.com/Conversation/IMMe.aspx?invitee=" + row.code + "@apps.messenger.live.com&mkt=zh-cn\"><img style=\"border-style: none;\" src=\"/public/plug/im/msn_icon/" + row.style + ".gif\"/></a>";
                            break;

                        case 5:
                            code = "<a target=\"_blank\" href=\"http://amos1.sh1.china.alibaba.com/msg.atc?v=1&uid=" + row.code + "\"><img border=\"0\" src=\"http://amos1.sh1.china.alibaba.com/online.atc?v=1&uid=" + row.code + "&s=" + row.style + "\"></a>";
                            break;
                    }
                    var m = new m_qq();
                    m.Name = row.name;
                    m.Code = code;
                    list.Add(m);
                }
                obj2 = list;
                cacheService.AddObject(cachekey, obj2, 0);
            }
            return (List<m_qq>)obj2;
        }

        /// <summary>
        /// 判断当前打开页是否首页
        /// </summary>
        /// <returns></returns>
        public static Boolean IsHomeCurrent()
        {
            if (UrlOper.GetRawUrl().ToLower() == "/" || UrlOper.GetRawUrl().ToLower() == "/default.aspx")
            {
                return true;
            }
            return false;
        }







































































































        public static Boolean IsFilter(String content, String filter)
        {
            if (filter.StrIsNullOrEmpty()) return true;
            var strArray = filter.SplitString("|");
            foreach (var row in strArray)
            {
                if (content.Contains(row)) return false;
            }
            return true;
        }

        /// <summary>
        /// 检查提交方式
        /// </summary>
        /// <returns></returns>
        public static Boolean IsOutSidePost()
        {
            return HttpContext.Current.Request.UrlReferrer != null && HttpContext.Current.Request.UrlReferrer.Host == HttpContext.Current.Request.ServerVariables["Server_Name"];
        }

        /// <summary>
        /// 禁止外部提交数据
        /// </summary>
        public static void CheckSubmit()
        {
            if (Mid((HttpContext.Current.Request.ServerVariables["HTTP_REFERER"]), 7, (HttpContext.Current.Request.ServerVariables["SERVER_NAME"]).Length) != HttpContext.Current.Request.ServerVariables["SERVER_NAME"])
            {
                HttpContext.Current.Response.Write("禁止从外部提交数据");
                HttpContext.Current.Response.End();
            }
        }

        public static Int32 BytLen(String str1)
        {
            return Encoding.Default.GetBytes(Trim(str1)).Length;
        }

        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static String LeftByt(String str, Int32 len)
        {
            if (((str == null) || (str.Length == 0)) || (len <= 0))
            {
                return string.Empty;
            }
            int num = 0;
            int num2 = 0;
            foreach (char ch in str)
            {
                if (ch > '\x007f')
                {
                    num += 2;
                }
                else
                {
                    num++;
                }
                if (num > len)
                {
                    str = str.Substring(0, num2);
                    return str;
                }
                num2++;
            }
            return str;
        }

        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static String LeftBytNoHtml(String str, Int32 len)
        {
            if (((str == null) || (str.Length == 0)) || (len <= 0))
            {
                return string.Empty;
            }
            str = RemoveHtml(str);
            int num = 0;
            int num2 = 0;
            foreach (char ch in str)
            {
                if (ch > '\x007f')
                {
                    num += 2;
                }
                else
                {
                    num++;
                }
                if (num > len)
                {
                    str = str.Substring(0, num2);
                    return str;
                }
                num2++;
            }
            return str;
        }

        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="intlen"></param>
        /// <param name="strSuffix"></param>
        /// <returns></returns>
        public static String LeftByt(String str, Int32 intlen, String strSuffix)
        {
            if (BytLen(str) > intlen)
            {
                return (LeftByt(str, intlen) + strSuffix);
            }
            return str;
        }

        #region 按字节长度截取字符串(支持截取带HTML代码样式的字符串)
        /// <summary>
        /// 按字节长度截取字符串(支持截取带HTML代码样式的字符串)
        /// </summary>
        /// <param name="param">将要截取的字符串参数</param>
        /// <param name="length">截取的字节长度</param>
        /// <param name="end">字符串末尾补上的字符串</param>
        /// <returns>返回截取后的字符串</returns>
        public static String subStringHTML(String param, Int32 length, String end)
        {
            if (param.StrIsNullOrEmpty()) return string.Empty;
            string Pattern = null;
            MatchCollection m = null;
            StringBuilder result = new StringBuilder();
            int n = 0;
            char temp;
            bool isCode = false; //是不是HTML代码
            bool isHTML = false; //是不是HTML特殊字符,如&nbsp;
            char[] pchar = param.ToCharArray();
            for (int i = 0; i < pchar.Length; i++)
            {
                temp = pchar[i];
                if (temp == '<')
                {
                    isCode = true;
                }
                else if (temp == '&')
                {
                    isHTML = true;
                }
                else if (temp == '>' && isCode)
                {
                    n = n - 1;
                    isCode = false;
                }
                else if (temp == ';' && isHTML)
                {
                    isHTML = false;
                }
                if (!isCode && !isHTML)
                {
                    n = n + 1;
                    //UNICODE码字符占两个字节
                    if (System.Text.Encoding.Default.GetBytes(temp + "").Length > 1)
                    {
                        n = n + 1;
                    }
                }
                result.Append(temp);
                if (n >= length)
                {
                    break;
                }
            }
            result.Append(end);
            //取出截取字符串中的HTML标记 
            string temp_result = Regex.Replace(result.ToString(), "(>)[^<>]*(<?)", "$1$2", RegexOptions.IgnoreCase);
            //去掉不需要结素标记的HTML标记 
            temp_result = Regex.Replace(temp_result, @"</?(area|base|basefont|body|br|col|colgroup|dd|dt|frame|head|hr|html|img|input|isindex|li|link|meta|option|p|param|tbody|td|tfoot|th|thead|tr)[^<>]*/?>", "", RegexOptions.IgnoreCase);
            //去掉成对的HTML标记 
            temp_result = Regex.Replace(temp_result, @"<([a-zA-Z]+)[^<>]*>(.*?)</\1>", "", RegexOptions.IgnoreCase);
            //用正则表达式取出标记 
            Pattern = ("<([a-zA-Z]+)[^<>]*>");
            m = Regex.Matches(temp_result, Pattern);
            ArrayList endHTML = new ArrayList();
            foreach (Match mt in m)
                endHTML.Add(mt.Result("$1"));
            //补全不成对的HTML标记 
            for (int i = endHTML.Count - 1; i >= 0; i--)
            {
                result.Append("</");
                result.Append(endHTML[i]);
                result.Append(">");
            }
            return result.ToString();
        }
        #endregion

        /// <summary>
        /// 清除前导和后面的空白字符
        /// </summary>
        /// <param name="object_0"></param>
        /// <returns></returns>
        public static String Trim(Object object_0)
        {
            if (object_0 == null)
            {
                return string.Empty;
            }
            return object_0.ToString().Trim();
        }

        /// <summary>
        /// 清除前导和后面的空白字符
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static String Trim(String s)
        {
            s = s ?? string.Empty;
            return s.Trim();
        }

        #region 格式化日期时间,0(yyyy-MM-dd),1(yyyy-MM-dd HH:mm:ss),2(yyyy/MM/dd),3(yyyy年MM月dd日),4(MM-dd),5(MM/dd),6(MM月dd日),7(yyyy-MM),8(yyyy/MM),9(yyyy年MM月),10(HH:mm:ss),11(yyyy-MM-dd HH:mm)
        /// <summary>
        /// 格式化日期时间,0(yyyy-MM-dd),1(yyyy-MM-dd HH:mm:ss),2(yyyy/MM/dd),3(yyyy年MM月dd日),4(MM-dd),5(MM/dd),6(MM月dd日),7(yyyy-MM),8(yyyy/MM),9(yyyy年MM月),10(HH:mm:ss),11(yyyy-MM-dd HH:mm),12(yyyy.MM.dd),13(yyyy年MM月dd日 HH:mm:ss),14(yyyy),15(yy.MM.dd),16(yy/MM/dd)
        /// </summary>
        /// <param name="dateMode">显示模式</param>
        /// <returns>0-9种模式的日期</returns>
        public static String GetNow(Int32 dateMode)
        {
            return FormatDate(DateTime.Now, dateMode);
        }

        /// <summary>
        /// 格式化日期时间,0(yyyy-MM-dd),1(yyyy-MM-dd HH:mm:ss),2(yyyy/MM/dd),3(yyyy年MM月dd日),4(MM-dd),5(MM/dd),6(MM月dd日),7(yyyy-MM),8(yyyy/MM),9(yyyy年MM月),10(HH:mm:ss),11(yyyy-MM-dd HH:mm),12(yyyy.MM.dd),13(yyyy年MM月dd日 HH:mm:ss),14(yyyy),15(yy.MM.dd),16(yy/MM/dd)
        /// </summary>
        /// <param name="dateTime">日期时间</param>
        /// <param name="dateMode">显示模式</param>
        /// <returns>0-9种模式的日期</returns>
        public static String FormatDate(DateTime dateTime, Int32 dateMode)
        {
            if (string.IsNullOrEmpty(dateTime.ToString())) return "";
            switch (dateMode)
            {
                case 0:
                    return dateTime.ToString("yyyy-MM-dd");

                case 1:
                    return dateTime.ToString("yyyy-MM-dd HH:mm:ss");

                case 2:
                    return dateTime.ToString("yyyy/MM/dd");

                case 3:
                    return dateTime.ToString("yyyy年MM月dd日");

                case 4:
                    return dateTime.ToString("MM-dd");

                case 5:
                    return dateTime.ToString("MM/dd");

                case 6:
                    return dateTime.ToString("MM月dd日");

                case 7:
                    return dateTime.ToString("yyyy-MM");

                case 8:
                    return dateTime.ToString("yyyy/MM");

                case 9:
                    return dateTime.ToString("yyyy年MM月");

                case 10:
                    return dateTime.ToString("HH:mm:ss");

                case 11:
                    return dateTime.ToString("yyyy-MM-dd HH:mm");

                case 12:
                    return dateTime.Year + "." + dateTime.Month + "." + dateTime.Day;

                case 13:
                    return dateTime.ToString("yyyy年MM月dd日 HH:mm:ss");

                case 14:
                    return dateTime.ToString("yyyy");

                case 15:
                    return Right(dateTime.Year.ToString(), 2) + "." + dateTime.Month + "." + dateTime.Day;

                case 16:
                    return Right(dateTime.Year.ToString(), 2) + "/" + dateTime.Month + "/" + dateTime.Day;
            }
            return dateTime.ToString();
        }
        #endregion

        #region 取左、中、右
        /// <summary>
        /// 取左边的字符
        /// </summary>
        /// <param name="sSource">字符串</param>
        /// <param name="iLength">要取长度</param>
        /// <returns></returns>
        public static String Left(String sSource, Int32 iLength)
        {
            if (!PageValidator.StrIsNullOrEmpty(sSource))
            {
                if (iLength > sSource.Length)
                {
                    return sSource;
                }
                else
                {
                    return sSource.Substring(0, iLength);
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 取右边的字符
        /// </summary>
        /// <param name="sSource">字符串</param>
        /// <param name="iLength">要取长度</param>
        /// <returns></returns>
        public static String Right(String sSource, Int32 iLength)
        {
            if (!PageValidator.StrIsNullOrEmpty(sSource))
            {
                if (iLength > sSource.Length)
                {
                    return sSource;
                }
                else
                {
                    return sSource.Substring(sSource.Length - iLength, iLength);
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 取中间的字符。
        /// </summary>
        /// <param name="sSource">字符串</param>
        /// <param name="iStart">开始长度</param>
        /// <param name="iLength">结束长度</param>
        /// <returns></returns>
        public static String Mid(String sSource, Int32 iStart, Int32 iLength)
        {
            if (!PageValidator.StrIsNullOrEmpty(sSource))
            {
                int iStartPoint = iStart > sSource.Length ? sSource.Length : iStart;
                return sSource.Substring(iStartPoint, iStartPoint + iLength > sSource.Length ? sSource.Length - iStartPoint : iLength);
            }
            return string.Empty;
        }

        #endregion

        #region 移除Html标记
        /// <summary>
        /// 移除Html标记
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static String RemoveHtml(Object content)
        {
            if (content.IsNull()) return String.Empty;
            return Regex.Replace(content.ToString(), @"<[^>]*>", string.Empty, RegexOptions.IgnoreCase);
        }
        #endregion

        #region 去除最前和最后的p
        /// <summary>
        /// 去除最前和最后的p
        /// </summary>
        /// <param name="Strings"></param>
        /// <returns></returns>
        public static String DelHtml(Object Strings1)
        {
            if (Strings1.IsNull()) return string.Empty;
            var Strings = Strings1.ToString();
            Strings = DelFirst(Strings, "<p>");
            Strings = DelLast(Strings, "</p>");
            return Strings;
        }
        #endregion

        #region 检测某一字符串的第一个字符是否与指定的 字符一致，相同则去掉这个字符
        /// <summary>
        /// 检测某一字符串的第一个字符是否与指定的 字符一致，相同则去掉这个字符
        /// </summary>
        /// <param name="Strings">字符串</param>
        /// <param name="Str"></param>
        /// <returns></returns>
        public static String DelFirst(String Strings, String Str)
        {
            if (Strings.StrIsNullOrEmpty()) return string.Empty;
            if (Strings.Length == 0)
            {
                throw new Exception("原始字符串长度为零");
            }
            if (Strings.StartsWith(Str))
            {
                return Strings.Substring(Str.Length, Strings.Length - Str.Length);
            }
            return Strings;
        }
        #endregion

        #region 检测某一字符串的最后一个字符是否与指定的 字符一致，相同则去掉这个字符
        /// <summary>
        /// 检测某一字符串的最后一个字符是否与指定的 字符一致，相同则去掉这个字符
        /// </summary>
        /// <param name="Strings">字符串</param>
        /// <param name="Str"></param>
        /// <returns></returns>
        public static String DelLast(String Strings, String Str)
        {
            if (Strings.StrIsNullOrEmpty()) return string.Empty;
            if (Strings.Length == 0)
            {
                throw new Exception("原始字符串长度为零");
            }
            if (Strings.EndsWith(Str))
            {
                return Strings.Substring(0, Strings.Length - Str.Length);
            }
            return Strings;
        }
        #endregion

        #region 生成指定大小的图片
        /// <summary>
        /// 生成指定大小的图片
        /// </summary>
        /// <param name="pic"></param>
        /// <param name="strZoom"></param>
        /// <returns></returns>
        public static string CacheImg(string pic, string strZoom)
        {
            var fileName = PathHelper.Map(pic);
            var filefold = PathHelper.Map(sys.Path.Root + "Cache/Pic/");
            if (!FileSystemObject.IsExist(filefold, FsoMethod.Folder))
            {
                FileSystemObject.Create(filefold, FsoMethod.Folder);
            }

            if (FileSystemObject.IsExist(fileName, FsoMethod.File))
            {
                var strArray = strZoom.SplitString("|");
                var dstW = TypeCon.StrToInt(strArray[0]);
                var dstH = TypeCon.StrToInt(strArray[1]);
                if (dstW > 0 && dstH > 0)
                {
                    var newFileName = sys.Path.Root + "Cache/Pic/" + dstW + "_" + dstH + "_" + DESEncrypt.Encrypt(fileName, 1) + "." + GetFileExtName(fileName);
                    if (!FileSystemObject.IsExist(PathHelper.Map(newFileName), FsoMethod.File))
                    {
                        var bgcolor = strArray[2];
                        if (bgcolor.StrIsNullOrEmpty())
                        {
                            bgcolor = Configs.find("valname=:v").set("v", "img_bgcolor").first().value;
                        }
                        if (Regex.Match(bgcolor, "/^#[a-zA-Z0-9]{3,6}$/i").Success)
                        {
                            bgcolor = "#ffffff";
                        }
                        var bordercolor = "#ffffff";
                        if (!strArray[3].StrIsNullOrEmpty())
                        {
                            bordercolor = strArray[3];
                        }
                        if (bordercolor.StrIsNullOrEmpty())
                        {
                            bordercolor = Configs.find("valname=:v").set("v", "img_bordcolor").first().value;
                        }
                        if (Regex.Match(bgcolor, "/^#[a-zA-Z0-9]{3,6}$/i").Success)
                        {
                            bordercolor = "#ffffff";
                        }
                        Thumbnail.MakeSquareImage(fileName, dstW, dstH, bordercolor, bgcolor, Thumbnail.PositionMode.Middle, PathHelper.Map(newFileName));
                    }
                    pic = newFileName;
                }
                else if (dstW == 0 && dstH == 0)
                {
                    return pic;
                }
                else
                {
                    decimal dstw;
                    decimal dsth;
                    var images = System.Drawing.Image.FromFile(fileName);
                    var aspectRatio = TypeCon.CDecimal(images.Width) / TypeCon.CDecimal(images.Height);
                    if (dstW == 0)
                    {
                        dstw = aspectRatio * dstH;
                    }
                    else dstw = dstW;
                    if (dstH == 0)
                    {
                        dsth = dstW / aspectRatio;
                    }
                    else dsth = dstH;
                    var newFileName = sys.Path.Root + "Cache/Pic/" + dstw + "_" + dsth + "_" + DESEncrypt.Encrypt(fileName, 1) + "." + GetFileExtName(fileName);
                    if (!FileSystemObject.IsExist(PathHelper.Map(newFileName), FsoMethod.File))
                    {
                        var bgcolor = strArray[2];
                        if (bgcolor.StrIsNullOrEmpty())
                        {
                            bgcolor = Configs.find("valname=:v").set("v", "img_bgcolor").first().value;
                        }
                        if (Regex.Match(bgcolor, "/^#[a-zA-Z0-9]{3,6}$/i").Success)
                        {
                            bgcolor = "#ffffff";
                        }
                        var bordercolor = "#ffffff";
                        if (strArray.Length == 4)
                        {
                            if (!strArray[3].StrIsNullOrEmpty())
                            {
                                bordercolor = strArray[3];
                            }
                            if (bordercolor.StrIsNullOrEmpty())
                            {
                                bordercolor = Configs.find("valname=:v").set("v", "img_bordcolor").first().value;
                            }
                            if (Regex.Match(bgcolor, "/^#[a-zA-Z0-9]{3,6}$/i").Success)
                            {
                                bordercolor = "#ffffff";
                            }
                        }
                        Thumbnail.MakeSquareImage(fileName, (int)dstw, (int)dsth, bordercolor, bgcolor, Thumbnail.PositionMode.Middle, PathHelper.Map(newFileName));
                    }
                    pic = newFileName;
                }
            }
            return pic;
        }

        /// <summary>
        /// 生成指定大小的图片，输出路径及长宽
        /// </summary>
        /// <param name="pic"></param>
        /// <param name="strZoom"></param>
        /// <returns></returns>
        public static string CacheImgOutWH(string pic, string strZoom)
        {
            var fileName = PathHelper.Map(pic);
            var filefold = PathHelper.Map(sys.Path.Root + "Cache/Pic/");
            if (!FileSystemObject.IsExist(filefold, FsoMethod.Folder))
            {
                FileSystemObject.Create(filefold, FsoMethod.Folder);
            }
            if (FileSystemObject.IsExist(fileName, FsoMethod.File))
            {
                var strArray = strZoom.SplitString("|");
                var dstW = TypeCon.StrToInt(strArray[0]);
                var dstH = TypeCon.StrToInt(strArray[1]);
                if (dstW > 0 && dstH > 0)
                {
                    var newFileName = sys.Path.Root + "Cache/Pic/" + dstW + "_" + dstH + "_" + DESEncrypt.Encrypt(fileName, 1) + "." + GetFileExtName(fileName);
                    if (!FileSystemObject.IsExist(PathHelper.Map(newFileName), FsoMethod.File))
                    {
                        var bgcolor = strArray[2];
                        if (bgcolor.StrIsNullOrEmpty())
                        {
                            bgcolor = Configs.find("valname=:v").set("v", "img_bgcolor").first().value;
                        }
                        if (Regex.Match(bgcolor, "/^#[a-zA-Z0-9]{3,6}$/i").Success)
                        {
                            bgcolor = "#ffffff";
                        }
                        var bordercolor = "#ffffff";
                        if (!strArray[3].StrIsNullOrEmpty())
                        {
                            bordercolor = strArray[3];
                        }
                        if (bordercolor.StrIsNullOrEmpty())
                        {
                            bordercolor = Configs.find("valname=:v").set("v", "img_bordcolor").first().value;
                        }
                        if (Regex.Match(bgcolor, "/^#[a-zA-Z0-9]{3,6}$/i").Success)
                        {
                            bordercolor = "#ffffff";
                        }
                        Thumbnail.MakeSquareImage(fileName, dstW, dstH, bordercolor, bgcolor, Thumbnail.PositionMode.Middle, PathHelper.Map(newFileName));
                    }
                    pic = newFileName;
                    return " src=\"" + pic + "\" style=\"width: " + dstW + "px; height: " + dstH + "px;\" ";
                }
                else if (dstW == 0 && dstH == 0)
                {
                    Image image = Image.FromStream(new MemoryStream(System.IO.File.ReadAllBytes(fileName)), false, false);
                    dstW = image.Width;
                    dstH = image.Height;
                    image.Dispose();
                    image = null;
                    return " src=\"" + pic + "\" style=\"width: " + dstW + "px; height: " + dstH + "px;\" ";
                }
                else
                {
                    decimal dstw;
                    decimal dsth;
                    var images = Image.FromStream(new MemoryStream(System.IO.File.ReadAllBytes(fileName)), false, false);
                    var aspectRatio = TypeCon.CDecimal(images.Width) / TypeCon.CDecimal(images.Height);
                    images.Dispose();
                    images = null;
                    if (dstW == 0)
                    {
                        dstw = aspectRatio * dstH;
                    }
                    else dstw = dstW;
                    if (dstH == 0)
                    {
                        dsth = dstW / aspectRatio;
                    }
                    else dsth = dstH;
                    var newFileName = sys.Path.Root + "Cache/Pic/" + (int)dstw + "_" + (int)dsth + "_" + DESEncrypt.Encrypt(fileName, 1) + "." + GetFileExtName(fileName);
                    if (!FileSystemObject.IsExist(PathHelper.Map(newFileName), FsoMethod.File))
                    {
                        var bgcolor = strArray[2];
                        if (bgcolor.StrIsNullOrEmpty())
                        {
                            bgcolor = Configs.find("valname=:v").set("v", "img_bgcolor").first().value;
                        }
                        if (Regex.Match(bgcolor, "/^#[a-zA-Z0-9]{3,6}$/i").Success)
                        {
                            bgcolor = "#ffffff";
                        }
                        var bordercolor = "#ffffff";
                        if (strArray.Length == 4)
                        {
                            if (!strArray[3].StrIsNullOrEmpty())
                            {
                                bordercolor = strArray[3];
                            }
                            if (bordercolor.StrIsNullOrEmpty())
                            {
                                bordercolor = Configs.find("valname=:v").set("v", "img_bordcolor").first().value;
                            }
                            if (Regex.Match(bgcolor, "/^#[a-zA-Z0-9]{3,6}$/i").Success)
                            {
                                bordercolor = "#ffffff";
                            }
                        }
                        Thumbnail.MakeSquareImage(fileName, (int)dstw, (int)dsth, bordercolor, bgcolor, Thumbnail.PositionMode.Middle, PathHelper.Map(newFileName));
                    }
                    pic = newFileName;
                    return " src=\"" + pic + "\" style=\"width: " + (int)dstw + "px; height: " + (int)dsth + "px;\" ";
                }
            }
            return " src=\"" + pic + "\" ";
        }
        #endregion

        #region 获取指定文件的扩展名
        /// <summary>
        /// 获取指定文件的扩展名
        /// </summary>
        /// <param name="fileName">指定文件名</param>
        /// <returns>扩展名</returns>
        public static string GetFileExtName(string fileName)
        {
            if (fileName.StrIsNullOrEmpty() || fileName.IndexOf('.') <= 0)
                return "";

            fileName = fileName.ToLower().Trim();
            return StringHelper.Right(fileName, fileName.Length - fileName.LastIndexOf('.') - 1);
        }
        #endregion

        #region 获取父级目录下的子目录ID
        /// <summary>
        /// 获取父级目录下的子目录ID
        /// </summary>
        /// <param name="tid"></param>
        /// <param name="isNow"></param>
        /// <returns></returns>
        public String GetParentChildStr(Int64 tid, Boolean isNow)
        {
            var modelTypeList = TypeList.findById(tid);
            if (modelTypeList != null)
            {
                return GetChildStr((int)modelTypeList.upid, isNow);
            }
            return string.Empty;
        }
        #endregion

        #region 获取子目录ID
        /// <summary>
        /// 获取子目录ID
        /// </summary>
        /// <param name="tid"></param>
        /// <param name="isNow"></param>
        /// <returns></returns>
        public String GetChildStr(Int64 tid, Boolean isNow)
        {
            var str = string.Empty;
            if (isNow)
            {
                str += tid + ",";
            }
            GetChildStr(tid, ref str);
            if (str.EndsWith(","))
            {
                str = str.Remove(str.Length - 1, 1);
            }
            return str;
        }
        #endregion

        #region 获取子目录ID
        /// <summary>
        /// 获取子目录ID
        /// </summary>
        /// <param name="tid"></param>
        /// <param name="str"></param>
        private void GetChildStr(Int64 tid, ref String str)
        {
            var list = TypeList.find(" upid =:u").set("u", tid).list();
            foreach (var row in list)
            {
                str += row.Id + ",";
                GetChildStr(row.Id, ref str);
            }
        }
        #endregion

        #region 获取父级目录
        /// <summary>
        /// 获取父级目录
        /// </summary>
        /// <param name="tid"></param>
        /// <param name="isNow"></param>
        /// <returns></returns>
        public string GetParentStr(Int64 tid, Boolean isNow)
        {
            var str = string.Empty;
            if (isNow)
            {
                str = tid + "," + str;
            }
            var modelTypeList = TypeList.findById(tid);
            if (modelTypeList != null)
            {
                GetParentStr((int)modelTypeList.upid, ref str);
            }
            if (str.EndsWith(","))
            {
                str = str.Remove(str.Length - 1, 1);
            }
            return str;
        }

        private void GetParentStr(Int64 upid, ref String str)
        {
            var list = TypeList.find(" Id=" + upid).list();
            foreach (var row in list)
            {
                str = row.Id + "," + str;
                GetParentStr((int)row.upid, ref str);
            }
        }
        #endregion

    }
}
